#pragma once

#include <iostream>
#include <string>
#include <cstdio>
#include <functional>


//这个文件和那边的类没有半毛钱关系，所以我们之间放到这里来

//serverio是回调函数
void serverio(int sock)
{
     //为该客户提供服务
     char buffer[1024];
     while(1)
     {
         //接收信息

         ssize_t n= read(sock, buffer,sizeof(buffer) - 1);
        // std::cout << "buffer = " << buffer << std::endl;

         if(n > 0)
         {
             //read完成后要给对方回信息
            // buffer[strlen (buffer) - 1 ] = 0;
             //std::string response = "我是服务器，好的我现在收到了你的信息";
             buffer[n] = 0;
             std::cout << buffer << std::endl;

             std::string str = buffer;
             //str += "返回给客户端#" ;

              write(sock,str.c_str(),str.size());
              //write(sock,buffer,sizeof (buffer));

              if( n == - 1)
              {
                 //logmessage("fatal",getpid(),"write failed");
                 exit(4);
              }
              //logmessage("write",getpid(),"write sucess");
         }
         else if(n == 0)
         {
             //说明客户端已经退出，我也可以退出了
             std::cout << "Cilent quie me too!" << std::endl;
             break;
         }
         //发送信息
     }
}

class Task
{
    using func_t = std::function<void(int)>;

public:
    Task()
    {
    }
    Task(int sock, func_t func)
        : _sock(sock), _callback(func)
    {
    }
    void operator()()
    {
        _callback(_sock);
    }

private:
    int _sock;
    func_t _callback;
};